package com.ipad.utils;

import android.content.Context;
import android.content.SharedPreferences;


import com.ipad.bean.User;
import com.ipad.bean.Warehouse;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by sev-14 on 2017/9/7.
 */

public class SharePerenceUtils {
    //用户名
    public static final String user_title = "User";
    public static final String user_id = "id";
    public static final String user_name = "name";
    public static final String user_department_name = "department_name";


    public static SharedPreferences getInstance(Context context, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, MODE_PRIVATE);
        return sharedPreferences;
    }

    public static String getStringValue(SharedPreferences sharedPreferences, String name) {
        String string = sharedPreferences.getString(name, "");
        return string;
    }


    public static void setToken(Context context, String token){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        SharedPreferences.Editor edit = user.edit();
        edit.putString("token",token);
        edit.commit();
    }
    public static void setPassword(Context context, String userName, String password){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        SharedPreferences.Editor edit = user.edit();
        edit.putString("userName",userName);
        edit.putString("password",password);
        edit.commit();
    }
    public static String getUserName(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString("userName","");
    }
    public static String getPassword(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString("password","");
    }
    public static String getToken(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString("token","");
    }



    public static String getUserId(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString(user_id,"");
    }
    public static void setUser(Context context, User user){
        SharedPreferences sp = context.getSharedPreferences("User", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(user_id,user.getUserId());
        edit.putString(user_name,user.getUsername());
        edit.putString("salt",user.getSalt());
        edit.putString("email",user.getEmail());
        edit.putString("mobile",user.getMobile());
        edit.putString("status",user.getStatus());
        edit.putString("createUserId",user.getCreateUserId());
        edit.putString("createTime",user.getCreateTime());
        edit.putString("department_id",user.getDeptId());
        edit.putString(user_department_name,user.getDeptName());
        edit.putString("realname",user.getRealname());
        edit.commit();
    }

    public static void setWarehouse(Context context, Warehouse wareHouseEntity){
        SharedPreferences sp = context.getSharedPreferences("User", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("warehouse_id",wareHouseEntity.getId()+"");
        edit.putString("warehouse_name",wareHouseEntity.getName());
        edit.commit();
    }
    public static void setIP(Context context, String ip){
        SharedPreferences sp = context.getSharedPreferences("IP", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("ip",ip);
        edit.commit();
    }
    public static String getIP(Context context){
        SharedPreferences sp = context.getSharedPreferences("IP", MODE_PRIVATE);
        String ip = sp.getString("ip", "");
        return ip;
    }


    public static String getWarehouseId(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString("warehouse_id","");
    }
    public static String getWarehouseName(Context context){
        SharedPreferences user = context.getSharedPreferences("User", MODE_PRIVATE);
        return user.getString("warehouse_name","");
    }


    public static void clear(Context context, String name) {
        SharedPreferences instance = getInstance(context, name);
        SharedPreferences.Editor clear = instance.edit().clear();
        clear.commit();

    }



}
